package com.jeesite.modules.merchant.service;

import com.jeesite.common.codec.DesUtils;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.codec.Sha1Utils;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.examine.dao.ExamineDao;
import com.jeesite.modules.examine.entity.Examine;
import com.jeesite.modules.merchant.dao.MerchantDao;
import com.jeesite.modules.merchant.entity.Merchant;
import com.jeesite.modules.sys.dao.UserDao;
import com.jeesite.modules.sys.dao.UserRoleDao;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.entity.UserRole;
import com.jeesite.modules.sys.service.OfficeService;
import com.jeesite.modules.sys.service.RoleService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class MerchantService extends CrudService<MerchantDao, Merchant> {


    public String judgeMerchantName(String name){
        return dao.judgeMerchantName(name);
    }

    public Integer judgeUserRepeat(String userCode){
        return dao.judgeUserRepeat(userCode);
    }

    @Transactional
    public void reviewData(Merchant merchant){
        // 根据审核结果  若同意  则自动创建一个账号
        Merchant merchant1 = new Merchant();
        merchant1.setId(merchant.getId());
        merchant1.setReviewStatus(merchant.getReviewStatus());
        merchant1.setRemarks(merchant.getRemarks());
        if (merchant.getReviewStatus().equals("1")){  // 同意
            Map map = new HashMap<>();
            map.put("status","0");
            map.put("createBy", UserUtils.getUser().getUserCode());
            map.put("userCode",merchant.getPhone());
            map.put("loginCode",merchant.getPhone());
            map.put("userName",merchant.getName());
            map.put("mobile",merchant.getPhone());
            map.put("userType","employee");
            map.put("refCode",map.get("userCode"));
            map.put("refName",merchant.getName());
            map.put("mgrType", EmpUser.MGR_TYPE_NOT_ADMIN);
            map.put("pwdSecurityLevel",0);
            map.put("setPwdUpdateDate",0);
            // 默认密码  123456
            String plain = EncodeUtils.decodeHtml("123456");
            byte[] salt = Sha1Utils.genSalt(8);
            byte[] hashPwd = Sha1Utils.sha1(plain.getBytes(), salt, 1024);
            String password = EncodeUtils.encodeHex(salt) + EncodeUtils.encodeHex(hashPwd);
            map.put("password",password);
            map.put("roleCode","user");
            map.put("officeCode",merchant.getPhone());
            map.put("officeName",merchant.getName());
            map.put("viewCode",merchant.getPhone());
            map.put("fullName",merchant.getName());
            int sortMax = dao.getOfficeTreeSortMax();
            int data = sortMax + 30;
            String result = String.format("%010d", data);
            map.put("treeSort",data);
            map.put("treeSorts",result+",");
            dao.saveUserInfo(map);
            dao.saveEmployeeInfo(map);
            dao.delUserRoleData(map);
            dao.saveUserRoleData(map);
            dao.saveOfficeData(map);
            merchant1.setUserId(merchant.getPhone());
        }
        super.update(merchant1);
    }

}
